layui.use(['form', "fsConfig", "fsCommon", "table"], function () {
    var form = layui.form,
        table = layui.table,
        layer = layui.layer,
        fsConfig = layui.fsConfig,
        fsCommon = layui.fsCommon,
        jumpLogin = $.result(fsConfig, "filters.401");

    var token = localStorage.getItem('cdce_serivce_token');
    if (token === "") {
        jumpLogin()
    }

    school_confrim = jQuery.parseJSON(localStorage.getItem("cdce_serivce_school_confrim"));
    console.log(school_confrim);
    if (school_confrim != null) {
        table.reload("subject_school_confrim", {data: school_confrim});
    }

    function callbackSubject(obj) {
        data = obj.data;
        for (var i = 0; i < data.length; i++) {
            $("#subject").append("<option value='" + data[i].id + "'>" + data[i].name + "</option>");
        }
        form.render();
    }


    fsCommon.get("/participate/v1alpha/subject", "", callbackSubject);


    form.on("select(subject_school_select)", function (data) {
        if (data.value != "") {
            fsCommon.get("/participate/v1alpha/school/" + data.value, "", callbackSubjectSchool);
        }
        localStorage.setItem('cdce_serivce_subject', JSON.stringify({
            "id": data.value,
            "text": data.elem[data.elem.selectedIndex].text
        }));
    });

    function callbackSubjectSchool(data) {
        table.reload("subject_school_reload", {data: data.data});
    }


    table.on('tool(subject_school)', function (obj) {
        var data = obj.data;
        if (obj.event === 'confrim') {
            localStorage.setItem('cdce_serivce_school', JSON.stringify(data));
            layer.open({
                type: 2,
                title: $("#subject  option:selected").text() + "-" + data.school,
                area: ['700px', '450px'],
                fixed: false, //不固定
                maxmin: true,
                content: 'school.html',
                end: function () {
                    school_confrim = jQuery.parseJSON(localStorage.getItem("cdce_serivce_school_confrim"));
                    if (school_confrim.length != 0) {
                        table.reload("subject_school_confrim", {
                            data: school_confrim
                        });
                    }
                }
            });
        }
    });


    $("#click_confrim").on("click", function () {
        school_confrim = jQuery.parseJSON(localStorage.getItem("cdce_serivce_school_confrim"));
        if (school_confrim == null) {
            layer.msg("无可确认数据");
            return false
        }
        data = [];
        for (i = 0; i < school_confrim.length; i++) {
            data.push({
                "course_id": school_confrim[i].course_id,
                "school_id": school_confrim[i].school_id,
                "time": school_confrim[i].time
            })
        }

        fsCommon.post("/participate/v1alpha/confirm", {"data": data}, callbackConfrim);
    });

    function callbackConfrim(obj) {
        layer.msg("报考成功，请进入缴费平台完成报名费的缴纳", {time: 1000}, function () {
            localStorage.setItem("cdce_serivce_school_confrim", null);
            table.reload("subject_school_confrim", {data: null});
        });
    }

});